package vsvm.gui.menus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import vsvm.gui.Environment;
import vsvm.gui.IUpdatable;
import vsvm.gui.forms.CreateClassifierDialog;
import vsvm.gui.forms.CreateDataSourceDialog;
import vsvm.gui.forms.CreateFilterDialog;
import vsvm.gui.forms.SelectDataSourceDialog;


public class ObjectMenu  extends JMenu implements IUpdatable {
	private final static String MO_CDS = "cds";
	private final static String MO_CDM = "cdm";
	private final static String MO_CF = "cf";
	private final static String MO_CC = "cc";
	
	private JMenuItem miCDS = null;
	private JMenuItem miCDM = null;
	private JMenuItem miCF = null;
	private JMenuItem miCC = null;
	
	private class ObjectMenuListener implements MenuListener {

		public void menuCanceled(MenuEvent arg0) {
		}

		public void menuDeselected(MenuEvent arg0) {
		}

		public void menuSelected(MenuEvent arg0) {
			boolean e = Environment.getEnvironment().getWorkspace() != null;
			miCDS.setEnabled(e);
			miCDM.setEnabled(e);
			miCF.setEnabled(e);
			miCC.setEnabled(e);
		}
		
	}
	
	private class ObjectMenuActionListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals(MO_CDS)) {
				CreateDataSourceDialog cdsd = new CreateDataSourceDialog(Environment.getEnvironment().getMainFrame());
				cdsd.setVisible(true);
				return;
			}
			
			if (e.getActionCommand().equals(MO_CDM)) {
				SelectDataSourceDialog d = new SelectDataSourceDialog(Environment.getEnvironment().getMainFrame());
				d.setVisible(true);
				return;
			}
			
			if (e.getActionCommand().equals(MO_CF)) {
				CreateFilterDialog d = new CreateFilterDialog(Environment.getEnvironment().getMainFrame());
				d.setVisible(true);
				return;
			}
			
			if (e.getActionCommand().equals(MO_CC)) {
				CreateClassifierDialog ccd = new CreateClassifierDialog();
				ccd.setVisible(true);	
				return;				
			}
		}		
	}
	
	public ObjectMenu() {
		super();
		this.setText("Object");
		createItems();
		addMenuListener(new ObjectMenuListener());
	}
	
	private void createItems() {
		this.removeAll();
		
		ActionListener ml = new ObjectMenuActionListener();
		
		miCDS = new JMenuItem("Create data source");
		miCDS.setActionCommand(MO_CDS);
		miCDS.addActionListener(ml);
		
		miCDM = new JMenuItem("Create data model");
		miCDM.setActionCommand(MO_CDM);
		miCDM.addActionListener(ml);
		
		miCF = new JMenuItem("Create filter");
		miCF.setActionCommand(MO_CF);
		miCF.addActionListener(ml);
		
		miCC = new JMenuItem("Create classifier");
		miCC.setActionCommand(MO_CC);
		miCC.addActionListener(ml);
		
		this.add(miCDS);
		this.add(miCDM);
		this.add(miCF);
		this.add(miCC);
		
		if (Environment.getEnvironment().getMainFrame() != null) {
		}
	}
	
	public void update() {
		createItems();
	}

}
